<?
 require_once("../inc/sku.php");
 
 //Confirms that the POST Exist and if it's going to create or update an entry
 if(isset($_POST['createNew']))
 {
	//Creates new Product
 	if($_POST['createNew'] === '1')
 	{
 		$results = addSKU($_POST);
	
		if($results === true)
		{
			header("location:index.php?inserted");
		}
		else
		{
			$sku = html_input_treatment($_POST);
		}
		
		$update = 1;
		$type_update = "New Product";
	}
	
	//Updates existing Product
	if($_POST['createNew'] === '0')
 	{
 		$results = editSKU($_POST);
	
		if($results === true)
		{

			if(isset($_SERVER['HTTP_REFERER']))
			{
				$cleaned_url = get_string_before($_SERVER['HTTP_REFERER'], "?");
				$cleaned_url .= "?id=".$_POST['id']."&updated";
				header("location:$cleaned_url");
			}
			else{
				
				header("location:index.php");
			}
			
		}
		else
		{
			$sku = html_input_treatment($_POST);
		}
		
		$update = 0;
		$heading = "Updated";
		$type_update = "Update Product";

	}
 	
 }
 //Obtains Product information for editing
 elseif(isset($_GET['id']))
 {
 	$sku = getSKU($_GET['id']);
	$sku = html_input_treatment($sku);

	
	$update = 0;
	$heading = "Updated";
	$type_update = "Update Product";
 }
 //Creates a blank Product for creating a new Product entry
 else 
 {
 	$sku = blank_sku();
	
	$update = 1;
	$heading = "New";
	$type_update = "New Product";
 }
 
 $vendors_results = getVendors();

?>

<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title><?=$title ?> <?=$type_update ?></title>
         <? require('../inc/header-info.php'); ?>
        <style>
			label.error {
				color: red;
				padding-left: .5em;
			}
        </style>
    <body>
        <div class="container">
            <? include('../inc/nav.php'); ?>
            
             <? if(isset($results)): ?>
            	<div class="alert alert-error">
                	<button class="close">x</button>
                    	<?=print_r($results)?>
                </div>
            <? endif; ?>
            
             <form id="addSku" name="addSku" method="post" action="addsku.php">
             	<h4>Enter <?=$heading ?> Product Information</h4>
             	<input type="hidden" name="createNew" value='<?=$update?>'> 
            	<input type="hidden" name="id" value='<?=$sku['id']?>'>
            		<label class="control-label">Vendor</label>
    				<div class="controls">
    					<select name='Vendor_id' class="required">
    						<? while($vendors = $vendors_results->fetch_assoc()): ?>
    							<option value='<?=$vendors['id']?>'
    								<? if(isset($_GET['id']))
  								{
  									if($sku['vendor_id'] == $vendors['id']){ ?>selected='selected'<? } 
									
  								}?>>
    								
    							<?=$vendors['name']?></option>					
                    		<? endwhile; ?>
							
						</select>
    				</div>			
            
					<label class="control-label" for="product_name">Product Name:</label>
					<div class="controls">
						<input type="text" id="product_name" class="required" name="product_name" placeholder="Product Name" value='<?=$sku['product_name']?>'>
					</div>
					
					<label class="control-label" for="description">Product Description:</label>
					<div class="controls">
						<input type="text" id="description" class="required" name="description" placeholder="Product Description" value='<?=$sku['description']?>'>
					</div>
				
					<label class="control-label" for="weight">Product Weight:</label>
					<div class="controls">
						<input type="text" id="weight" class="required" name="weight" placeholder="Product Weight" value='<?=$sku['weight']?>'>
					</div>
            	<div class="form-actions">
  					<button type="submit" class="btn btn-primary"><?=$type_update?></button>
					<a class="btn" href="<?=$url?>sku/index.php">Cancel</a>
				</div>	
            </form> 
		</div>
	<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>	
	<script>
		$(function(){
			
			$("#addSku").validate();
			
		});
		
		
	</script>
	
	</body>
</html>
